package cn.otrue.patienthealthmanager.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import cn.otrue.patienthealthmanager.R;

public class SwitchButton extends RelativeLayout {

	private Context context;

	public boolean open = true;

	private ImageView iv_switch_button_open;

	public SwitchButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		initView();
		open = attrs.getAttributeBooleanValue(null, "open", true);
		openButton(open);

	}

	public void openButton(boolean isOpen) {
		if (isOpen) {
			iv_switch_button_open.setImageDrawable(context.getResources()
					.getDrawable(R.drawable.switchon));
		} else {
			iv_switch_button_open.setImageDrawable(context.getResources()
					.getDrawable(R.drawable.switchoff));
		}
	}

	private void initView() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View main = inflater.inflate(R.layout.item_switch_button, this);
		iv_switch_button_open = (ImageView) main
				.findViewById(R.id.iv_switch_button_open);

	}

	public void changeOpen() {
		open = !open;
		openButton(open);
	}

}
